
package com.twodstar.photobattle;

import java.util.HashMap;

import android.app.Activity;
import android.support.v4.app.FragmentManager;
import android.util.Log;

import com.parse.FunctionCallback;
import com.parse.ParseCloud;
import com.parse.ParseException;
import com.parse.ParseInstallation;
import com.parse.ParseObject;
import com.parse.ParsePush;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.parse.SaveCallback;




/**
 * Use a custom Application class to pass state data between Activities.
 */
public class DataManager 
{
	

	public static void SaveBattle(String P1, final String P2, String PhotoID_P1, String PhotoID_P2, int InitialLikeCount_P2,
								  String Photo_URL_1, String Photo_URL_2, final String Name1, String Name2	)
	{
		
		if(P1 == null)
		{
			Log.v("game","P1 is null");
			return;
		}
		if(P2 == null)
		{
			Log.v("game","P2 is null");
			return;
		}
		if(PhotoID_P1 == null)
		{
			Log.v("game","PhotoID_P1 is null");
			return;
		}
		if(PhotoID_P2 == null)
		{
			Log.v("game","PhotoID_P2 is null");
			return;
		}
                 
		ParseObject pParseObj = new ParseObject("Battles");
		pParseObj.put("P1", P1);
		pParseObj.put("P2", P2);
		pParseObj.put("PhotoID_P1", PhotoID_P1);
		pParseObj.put("PhotoID_P2", PhotoID_P2);
		
		long StartDate = SntpClient.GetTimeUTC();		
		pParseObj.put("StartDate",  StartDate );		
		pParseObj.put("EndDate",  StartDate + 1000 * 60 * 60 * 23  ); // 23 hours	
		
		pParseObj.put("WinnerID", "0");
		pParseObj.put("InitialLikeCount_P2", InitialLikeCount_P2);
		pParseObj.put("Photo_URL_1", Photo_URL_1);
		pParseObj.put("Photo_URL_2", Photo_URL_2);
		pParseObj.put("Name1", Name1);
		pParseObj.put("Name2", Name2);
		pParseObj.put("Finished_Like1", 0);
		pParseObj.put("Finished_Like2", 0);
		
		
		
		pParseObj.saveInBackground(new SaveCallback() 
								  {
			   						public void done(ParseException e) 
			   						{
			   							if (e == null) 
			   							{
			   							 // force refresh
			   								Fragment_Battles.ForceRefreshBattleList(1);
			   								SendPush(P2, Name1 + " is challenging your photo!");
			   								
			   							} 
			   							else 
			   							{
			   							
			   							}
			   						}
								  }
								  );
		

		
	}
	
	private static void SendPush(String FBID, 
								 String Message
								 )
	{
		// Create our Installation query
		ParseQuery pushQuery = ParseInstallation.getQuery();
		pushQuery.whereEqualTo("FBID", "");
		 
		// Send push notification to query
		ParsePush push = new ParsePush();
		push.setQuery(pushQuery); // Set our Installation query
		push.setMessage(Message);
		push.sendInBackground();
	}
	
	public static void UpdateWinnerID(	String objectId, 
										String WinnerID, 
										String Player1, 
										String Player2,
										int Finished_Like1,
										int Finished_Like2
										)
	{
		HashMap<String, Object> Para = new HashMap<String, Object>();
		Para.put("objectId", objectId);
		Para.put("WinnerID", WinnerID);
		Para.put("Player1", Player1);
		Para.put("Player2", Player2);
		Para.put("Finished_Like1", Finished_Like1);
		Para.put("Finished_Like2", Finished_Like2);
		
		ParseCloud.callFunctionInBackground("UpdateWinnerID", Para, new FunctionCallback<String>() 
		{
		  public void done(String result, ParseException e) 
		  {
		    if (e == null) 
		    {
		    	Log.i("game","updating WinnerID Successful");
		    	Fragment_Battles.ForceRefreshBattleList(3);
		    }
		  }
		});
		
	}
	
	public static void InitNewParseUser(ParseUser PU,
										final String FBID,
										String Name,
										String City,
										String Country)
			
	{
		PU.put("FBID", FBID);
		PU.put("TotalBattle", 0);
		PU.put("BattleWin", 0);
		PU.put("LevelNum", 1);    
		PU.put("City", City);
		PU.put("Country", Country);
		PU.put("Points", 0);
		PU.setUsername(Name);
		
		PU.saveInBackground(new SaveCallback() 
		  {
				public void done(ParseException e) 
				{
					if (e == null) 
					{
					 // force refresh
						ParseInstallation PObj = ParseInstallation.getCurrentInstallation();
						if(PObj != null)
						{
							PObj.put("FBID", FBID);
							PObj.saveInBackground();
						}
					} 
					else 
					{
						Log.i("game", e.toString());
					}
				}
		  });				// initialize some fields
		
		
	}
	
	public static void SaveCounterAttack(
											ParseObject pParseObj,
											final String PlayerID1,
											final String PlayerName2,
											String PhotoID_P2,
											String Photo_URL_2
										)
	{
		if(pParseObj == null)
			return;
		
		pParseObj.put("PhotoID_P2", PhotoID_P2);
		pParseObj.put("Photo_URL_2", Photo_URL_2);
		pParseObj.put("InitialLikeCount_P2", 0);
		long StartDate = SntpClient.GetTimeUTC();		
		pParseObj.put("StartDate",  StartDate );		
		pParseObj.put("EndDate",  StartDate + 1000 * 60 * 60 * 23  ); // 23 hours	
		
		pParseObj.saveInBackground(new SaveCallback() 
		  {
				public void done(ParseException e) 
				{
					if (e == null) 
					{
					 // force refresh						
						Fragment_Battles.ForceRefreshBattleList(1);
						SendPush( PlayerID1 , PlayerName2 + " is attacking you back!" );						
					} 
					else 
					{
					
					}
				}
		  }
		  );
		
	}
    
}
